﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emoney.Spider.Basic;
using System.Data;
using System.Data.SqlClient;

namespace Emoney.Cms.Spider
{
    public class CmsAccess
    {
        /// <summary>
        /// 新采集一条新闻链接
        /// </summary>
        /// <param name="et">Cms数据实体</param>
        /// <returns></returns>
        public int InsertNews(CmsEnty et)
        {
            lock (this)
            {
                int rel = 0;
                try
                {
                    SqlParameter[] para = new SqlParameter[6];
                    para[0] = new SqlParameter("@LINK", et.LINK);
                    para[1] = new SqlParameter("@TITLE", et.TITLE);
                    para[2] = new SqlParameter("@PTIME", et.PTIME);
                    para[3] = new SqlParameter("@CTIME", et.CTIME);
                    para[4] = new SqlParameter("@FLAG", et.FLAG);
                    para[5] = new SqlParameter("@CID", et.CID);

                    rel = SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, CommandType.StoredProcedure, "sp_InsertNews", para);
                    return rel;
                }
                catch (Exception ex)
                {
                    return -1;
                }
            }
        }

        /// <summary>
        /// 更新文章内容
        /// </summary>
        /// <param name="et">Cms数据实体</param>
        /// <returns></returns>
        public int UpdateNews(CmsEnty et)
        {
            lock (this)
            {
                int rel = 0;
                try
                {
                    SqlParameter[] para = new SqlParameter[10];
                    para[0] = new SqlParameter("@ID", et.ID);
                    para[1] = new SqlParameter("@TITLE", et.TITLE);
                    para[2] = new SqlParameter("@AUTHOR", et.AUTHOR);
                    para[3] = new SqlParameter("@SOURCE", et.SOURCE);
                    para[4] = new SqlParameter("@KEYWORD", et.KEYWORD);
                    para[5] = new SqlParameter("@SUMMARY", et.SUMMARY);
                    para[6] = new SqlParameter("@CONTENT", et.CONTENT);
                    para[7] = new SqlParameter("@PTIME", et.PTIME);
                    para[8] = new SqlParameter("@STOCKCODE", et.STOCKCODE);
                    para[9] = new SqlParameter("@FLAG", et.FLAG);

                    rel = SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, CommandType.StoredProcedure, "sp_UpdateNews", para);
                    return rel;
                }
                catch (Exception ex)
                {
                    return -1;
                }
            }
        }

        /// <summary>
        /// 新闻类数据表
        /// </summary>
        /// <param name="_cid">类别ID</param>
        /// <returns>返回此类表数据</returns>
        public DataTable TableNewsByCID(int _cid)
        {
            DataTable _dt = new DataTable();
            try
            {
                string key = string.Format(CacheKey.News_CACHE_KEY, "DB", _cid.ToString());
                if (Cache.getCache(key) != null)
                    return (DataTable)Cache.getCache(key);
                else
                {
                    SqlParameter[] para = new SqlParameter[1];
                    para[0] = new SqlParameter("@CID", _cid);
                    _dt = SQLHelper.ExecuteSet(SQLHelper.StrConn, CommandType.StoredProcedure, "sp_SelNewsLIST", para).Tables[0];
                    if (_dt.Rows.Count > 0)
                        Cache.addCache(key, _dt, 300);
                }
                return _dt;
            }
            catch (Exception ex)
            {
                return _dt;
            }
        }

        /// <summary>
        /// 新闻List数据
        /// </summary>
        /// <param name="_cid">类别ID</param>
        /// <returns>返回此类List数据</returns>
        public List<CmsEnty> ListAcc_NewsByCID(int _cid)
        {
            List<CmsEnty> list = new List<CmsEnty>();
            try
            {
                DataTable _dt = TableNewsByCID(_cid);
                for (int i = 0; i < _dt.Rows.Count; i++)
                {
                    CmsEnty et = new CmsEnty();
                    et.ID = (int)_dt.Rows[i]["ID"];
                    et.LINK = _dt.Rows[i]["LINK"].ToString();
                    et.TITLE = OTO.ToStr(_dt.Rows[i]["TITLE"]);
                    et.CTIME = OTO.ToDate(_dt.Rows[i]["CTIME"]);
                    et.PTIME = OTO.ToDate(_dt.Rows[i]["PTIME"]);
                    et.SOURCE = _dt.Rows[i]["SOURCE"].ToString();
                    et.FLAG = (int)_dt.Rows[i]["FLAG"];
                    et.CID = (int)_dt.Rows[i]["CID"];
                    list.Add(et);
                }
            }
            catch (Exception ex)
            { }
            return list;
        }

        /// <summary>
        /// 取得当前ID的文章内容
        /// </summary>
        /// <param name="ID">文章ID</param>
        /// <returns></returns>
        public CmsEnty NewsContentByID(int ID)
        {
            CmsEnty et = new CmsEnty();
            try
            {
                DataTable _dt = new DataTable();
                SqlParameter[] para = new SqlParameter[1];
                para[0] = new SqlParameter("@ID", ID);
                _dt = SQLHelper.ExecuteSet(SQLHelper.StrConn, CommandType.StoredProcedure, "sp_NewsContentByID", para).Tables[0];
                if (_dt.Rows.Count > 0)
                {
                    et.ID = ID;
                    et.LINK = _dt.Rows[0]["LINK"].ToString();
                    et.TITLE = OTO.ToStr(_dt.Rows[0]["TITLE"]);
                    et.AUTHOR = OTO.ToStr(_dt.Rows[0]["AUTHOR"]);
                    et.SOURCE = OTO.ToStr(_dt.Rows[0]["SOURCE"]);
                    et.KEYWORD = OTO.ToStr(_dt.Rows[0]["KEYWORD"]);
                    et.SUMMARY = OTO.ToStr(_dt.Rows[0]["SUMMARY"]);
                    et.CONTENT = OTO.ToStr(_dt.Rows[0]["CONTENT"]);
                    et.PTIME = OTO.ToDate(_dt.Rows[0]["PTIME"]);
                    et.CTIME = OTO.ToDate(_dt.Rows[0]["CTIME"]);
                    et.STOCKCODE = OTO.ToStr(_dt.Rows[0]["STOCKCODE"]);
                    et.CID = OTO.ToInt(_dt.Rows[0]["CID"],0);
                }
            }
            catch
            { }
            return et;
        }
        

        public DataTable TableNewsAll()
        {
            DataTable _dt = new DataTable();
            try
            {
                string key = string.Format(CacheKey.News_CACHE_KEY, "DB", "All");
                if (Cache.getCache(key) != null)
                    return (DataTable)Cache.getCache(key);
                else
                {
                    _dt = SQLHelper.ExecuteSet(SQLHelper.StrConn, CommandType.StoredProcedure, "sp_SelNewsListALL", null).Tables[0];
                    if (_dt.Rows.Count > 0)
                        Cache.addCache(key, _dt, 300);
                }
                return _dt;
            }
            catch (Exception ex)
            {
                return _dt;
            }
        }


        public List<CmsEnty> ListAcc_NewsAll()
        {
            List<CmsEnty> list = new List<CmsEnty>();
            try
            {
                DataTable _dt = TableNewsAll();
                for (int i = 0; i < _dt.Rows.Count; i++)
                {
                    CmsEnty et = new CmsEnty();
                    et.ID = (int)_dt.Rows[i]["ID"];
                    et.LINK = _dt.Rows[i]["LINK"].ToString();
                    et.TITLE = OTO.ToStr(_dt.Rows[i]["TITLE"]);
                    et.CTIME = OTO.ToDate(_dt.Rows[i]["CTIME"]);
                    et.PTIME = OTO.ToDate(_dt.Rows[i]["PTIME"]);
                    et.SOURCE = _dt.Rows[i]["SOURCE"].ToString();
                    et.FLAG = (int)_dt.Rows[i]["FLAG"];
                    et.CID = (int)_dt.Rows[i]["CID"];
                    list.Add(et);
                }
            }
            catch (Exception ex)
            { }
            return list;
        }

    }
}
